import 'package:valovault_reminder/common/ui/val_ui_kit_btn.dart';
import 'package:valovault_reminder/common/ui/val_auto_fit_screen_size.dart';
import 'package:valovault_reminder/common/util/val_router_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../config/val_assets_name.dart';
import 'val_ui_kit_base_colors.dart';
import 'val_fast_string_tool.dart';
import 'val_common_text.dart';

class ValUIKitStatusBar {
  static const SystemUiOverlayStyle _light = SystemUiOverlayStyle(
    systemNavigationBarColor: Color(0xFFFFFFFF),
    systemNavigationBarDividerColor: null,
    statusBarColor: Color(0x00000000),
    systemNavigationBarIconBrightness: Brightness.light,
    statusBarIconBrightness: Brightness.light,
    statusBarBrightness: Brightness.dark,
  );

  static const SystemUiOverlayStyle _dark = SystemUiOverlayStyle(
    systemNavigationBarColor: Color(0xFFFFFFFF),
    systemNavigationBarDividerColor: null,
    statusBarColor: Color(0x00000000),
    systemNavigationBarIconBrightness: Brightness.light,
    statusBarIconBrightness: Brightness.dark,
    statusBarBrightness: Brightness.light,
  );

  static void setStatusBar(bool white) {
    SystemChrome.setSystemUIOverlayStyle(white ? _dark : _light);
  }

  static const SystemUiOverlayStyle light = _light;
  static const SystemUiOverlayStyle dark = _dark;
}

class ValBaseUIKitRoot extends StatelessWidget {
  const ValBaseUIKitRoot(
      this.child, {
        Key? key,
        this.title,
        this.titleStr,
        this.leading,
        this.leadingWidth,
        this.titleColor = ValUIKitBaseColors.textLeveOne,
        this.backIcon = '${ValAssetsName.ASSETS_MINE}val_my_home_back_btn.png',//${CsConfig.ASSETS_Message}message_nav_back.png',
        this.backColor,
        this.onWillPop,
        this.actions,
        this.elevation = 0.5,
        this.scaffoldBackgroundColor = Colors.white,
        this.appbarBackgroundColor = ValUIKitBaseColors.white,
        this.statusBarStyle = ValUIKitStatusBar.dark,
        this.appbarHeight,
        this.resizeToAvoidBottomInset = false,
        this.touchHideKeyboard = true,
        this.onHideKeyboardCall,
        this.hideBackBtn = false,
        this.titleSpacing,
        this.centerTitle = true,
        this.drawer,
        this.scaffoldKey,
      }) : super(key: key);

  final Widget child;
  final Widget? title;
  final Widget? leading;
  final double? leadingWidth;
  final String? titleStr;
  final Color titleColor;
  final String backIcon;
  final Color? backColor;
  final Function()? onWillPop;
  final List<Widget>? actions;
  final double elevation;
  final Color scaffoldBackgroundColor;
  final Color appbarBackgroundColor;
  final double? appbarHeight;
  final bool? resizeToAvoidBottomInset;
  final SystemUiOverlayStyle statusBarStyle;
  final bool touchHideKeyboard;
  final VoidCallback? onHideKeyboardCall;
  final bool hideBackBtn;
  final double? titleSpacing;
  final bool centerTitle;
  final Widget? drawer;
  final Key? scaffoldKey;

  @override
  Widget build(BuildContext context) {
    PreferredSizeWidget? appBar;
    if (title != null || ValFastStringTool.isNotEmpty(titleStr)) {
      Widget? titleWidget = title;
      if (ValFastStringTool.isNotEmpty(titleStr)) {
        titleWidget = ValCommonText.moreLine(titleStr!, 17, titleColor, fontWeight: FontWeight.w600);
      }
      Function() back = () {};
      if (onWillPop != null) {
        back = onWillPop!;
      }
      appBar = ValBaseUIRootUtils.buildAppbar(
        context,
        title: titleWidget,
        leading: hideBackBtn ? null : leading ?? _buildBackBtn() ,
        leadingWidth: leadingWidth,
        actions: actions,
        appbarHeight: appbarHeight ?? 44,
        elevation: elevation,
        backgroundColor: appbarBackgroundColor,
        statusBarStyle: statusBarStyle,
        titleSpacing: titleSpacing,
        centerTitle: centerTitle,
      );
    }
    var body = child;
    return ValBaseUIRootUtils.buildRoot(
      context,
      body,
      appBar: appBar,
      backgroundColor: scaffoldBackgroundColor,
      resizeToAvoidBottomInset: resizeToAvoidBottomInset,
      statusBarStyle: statusBarStyle,
      touchHideKeyboard: touchHideKeyboard,
      onHideKeyboardCall: onHideKeyboardCall,
      drawer: drawer,
      scaffoldKey: scaffoldKey,
    );
  }

  Widget _buildBackBtn() {
    return ValUIKitBtn(Container(
      width: 40.w,
      height: 40.w,
      child: Center(child: Image.asset(backIcon,width: 25.w,),),
    ), () => ValRouterManager.close());
  }
}

class ValBaseUIRootUtils {
  ///构建整体宽高
  static Widget buildRoot(
      BuildContext context,
      Widget body, {
        PreferredSizeWidget? appBar,
        Color backgroundColor = Colors.white,
        bool? resizeToAvoidBottomInset = false,
        SystemUiOverlayStyle statusBarStyle = ValUIKitStatusBar.dark,
        bool touchHideKeyboard = true,
        VoidCallback? onHideKeyboardCall,
        Widget? drawer,
        Key? scaffoldKey,
      }) {
    if (appBar == null) {
      body = AnnotatedRegion<SystemUiOverlayStyle>(value: statusBarStyle, child: body);
    }
    body = Scaffold(
      key: scaffoldKey,
      resizeToAvoidBottomInset: resizeToAvoidBottomInset,
      backgroundColor: backgroundColor,
      appBar: appBar,
      body: body,
      drawer: drawer,
      drawerEdgeDragWidth: 30,
    );
    if (touchHideKeyboard) {
      body = GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          FocusManager.instance.primaryFocus?.unfocus();
          onHideKeyboardCall?.call();
        },
        child: body,
      );
    }
    return body;
  }

  ///构建头部操作栏
  static PreferredSizeWidget buildAppbar(
      BuildContext context, {
        double? appbarHeight,
        Widget? title,
        Widget? leading,
        double? leadingWidth,
        List<Widget>? actions,
        SystemUiOverlayStyle statusBarStyle = ValUIKitStatusBar.dark,
        double elevation = 0.5,
        Color backgroundColor = ValUIKitBaseColors.white,
        double? titleSpacing,
        bool centerTitle = true,
      }) {
    return PreferredSize(
      preferredSize: Size.fromHeight(appbarHeight ?? 44),
      child: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: backgroundColor,
        elevation: elevation,
        shadowColor: ValUIKitBaseColors.line,
        centerTitle: centerTitle,
        title: title,
        leading: leading,
        leadingWidth: leadingWidth,
        actions: actions,
        systemOverlayStyle: statusBarStyle,
        titleSpacing: titleSpacing,
      ),
    );
  }
}